package com.example.mq.mqserver.core;

import com.example.mq.common.ConsumerEnv;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: DongGuoZhen
 * @Date: 2024/02/14/10:55
 * @Description: MSG = message 表示存储消息的队列
 */
public class MSGQueue {
    //表示队列的身份标识
    private String name;
    //表示队列是否持久化  true表示持久化  false表示不持久化
    private boolean durable = false;


    //true表示这个队列只能被一个消费者使用(别人不能使用) false则是大家都能使用
    private boolean exclusive = false;
    //true表示没有人使用就自动删除 false表示不自动删除
    private boolean autoDelete = false;
    //扩展参数
    private Map<String, Object> arguments = new HashMap<>();

    //当前队列都要那些消费者订阅
    private List<ConsumerEnv> consumerEnvList = new ArrayList<>();
    //记录当前取到的第几个消费者,方便轮询调用
    private AtomicInteger  consumerSeq =  new AtomicInteger(0);

    //添加一个新的订阅者
    public void addConsumerEnv(ConsumerEnv consumerEnv) {
        synchronized (this) {
            consumerEnvList.add(consumerEnv);
        }
    }

    //订阅者删除暂时不考虑
    //挑选一个订阅者,用来处理当前消息 (按照轮询的方式)
    public ConsumerEnv choseConsumer() {
        //
        if(consumerEnvList.size() ==0) {
            //该队列是没有订阅的
            return null;
        }
        //计算下当前要去的元素的下标
        int index = consumerSeq.get() % consumerEnvList.size();
        consumerSeq.getAndIncrement();
        return consumerEnvList.get(index);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

    public String getArguments() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "{}";
    }

    public void setArguments(String argumentsJson) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            this.arguments = objectMapper.readValue(argumentsJson, new TypeReference<HashMap<String, Object>>() {
            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    //在这里再提供一组get and set 方法  针对 arguments
    //在java代码内部使用
    public Object getArguments(String key) {
        return arguments.get(key);
    }

    public void setArguments(String key, Object value) {
        arguments.put(key, value);
    }

    public void setArguments(Map<String, Object> arguments) {
        this.arguments = arguments;
    }
}
